﻿using System.Collections.Generic;
using System.Linq;
using Neptuno.Model;

namespace Neptuno.Component
{
    public class LineasComponenent : BaseComponent
    {
        public Lineas AgregarLinea(Lineas Linea)
        {
            Linea.idAgencia = 1;
            Linea.habilitada = true;
            List<Lineas> Lineas = new List<Model.Lineas>();
            Context.Lineas.AddObject(Linea);
            Context.TryToSave();

            return Linea;
        }

        public bool Borrar(int idLinea)
        {
            Lineas Linea = Context.Lineas.Single(t => t.idLinea == idLinea);
            Linea.habilitada = false;
            return Context.TryToSave();
        }

        public bool Habilitar(int idLinea)
        {
            Lineas Linea = Context.Lineas.Single(t => t.idLinea == idLinea);
            Linea.habilitada = true;
            return Context.TryToSave();
        }

        public List<Lineas> Listar(bool EsHabilitada)
        {
            Context.Agencias.ToList();
            List<Lineas> Lista = Context.Lineas.Where(t => t.habilitada == EsHabilitada).ToList();
            Lista.ForEach(t => t.Agencias.Lineas = null);
            return Lista;
        }

        public Lineas GetById(int idLinea)
        {
            return Context.Lineas.SingleOrDefault(t => t.idLinea == idLinea);
        }

        public bool Actualizar(Lineas Linea)
        {
            if (Linea.idLinea != 0)
            {
                Lineas myLinea = Context.Lineas.SingleOrDefault(t => t.idLinea == Linea.idLinea);
                myLinea.nombre = Linea.nombre;
                myLinea.abreviatura = Linea.abreviatura;
                myLinea.email = Linea.email;

                return Context.TryToSave();
            }
            else
                return true;
        }
    }
}